package controler;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.DanhmucModel;
import model.QuangcaoModel;
import model.SanphamModel;

import entity.Quangcao;
import entity.Sanpham;
import entity.danhmuc;

import entity.Khuyenmai;

import model.KhuyenmaiModel;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Servlet implementation class listDanhmuc
 */
@WebServlet("/trangchu")
public class listDanhmuc extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public listDanhmuc() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws SQLException 
	 * @throws IOException 
	 * @throws ServletException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws SQLException, ServletException, IOException{
    	response.setContentType("text/html;charset=UTF-8");
    	List<danhmuc> list1 = new ArrayList<danhmuc>();
    	DanhmucModel model = new DanhmucModel();
    	list1 = model.getlistdanhmuc();
    	HttpSession session;
    	session = request.getSession();
    	session.setAttribute("list", list1);
    	
    	List<Khuyenmai> listkm = new ArrayList<Khuyenmai>();
    	KhuyenmaiModel modelkm = new KhuyenmaiModel();
    	listkm = modelkm.getlistkhuyenmai("select TOP 1 * from khuyenmai order by MAKM desc");
    	session.setAttribute("listkm", listkm);
    	
    	String optionlist = model.getOptions(true, false, "");
    	session.setAttribute("optionlist", optionlist);
    	
    	String strSearch = "select top 9 * from SANPHAM order by NGAYDANG asc";
    	SanphamModel spmodel = new SanphamModel();
    	List<Sanpham> listgiay = new ArrayList<Sanpham>();
    	listgiay= spmodel.timsanpham(strSearch);
    	request.setAttribute("listgiay", listgiay);
    	
    	strSearch = "select TOP 4 tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua ,count(cthd.masp) as sl from SANPHAM,CTHD,hoadon where MADANhMUC = '1'and cthd.mahd = hoadon.mahd and sanpham.masp = cthd .masp group by tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua order by sl desc";
    	List<Sanpham> listthoitrang = new ArrayList<Sanpham>();
    	listthoitrang = spmodel.timsanpham(strSearch);
    	session.setAttribute("listthoitrang", listthoitrang);
    	
    	strSearch = "select TOP 4 tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua ,count(cthd.masp) as sl from SANPHAM,CTHD,hoadon where MADANhMUC = '3'and cthd.mahd = hoadon.mahd and sanpham.masp = cthd .masp group by tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua order by sl desc";
    	List<Sanpham> listdientu = new ArrayList<Sanpham>();
    	listdientu = spmodel.timsanpham(strSearch);
    	session.setAttribute("listdientu", listdientu);
    	
    	strSearch = "select TOP 4 tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua ,count(cthd.masp) as sl from SANPHAM,CTHD,hoadon where MADANhMUC = '4'and cthd.mahd = hoadon.mahd and sanpham.masp = cthd .masp group by tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua order by sl desc";
    	List<Sanpham> listanuong = new ArrayList<Sanpham>();
    	listanuong = spmodel.timsanpham(strSearch);
    	session.setAttribute("listanuong", listanuong);
    	
    	strSearch = "select TOP 4 tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua ,count(cthd.masp) as sl from SANPHAM,CTHD,hoadon where MADANhMUC = '5'and cthd.mahd = hoadon.mahd and sanpham.masp = cthd .masp group by tensp,mota,giaban,discount,urlhinh,urlhinh2,urlhinh3,sanpham.masp,madanhmuc,thuonghieu,sanpham.soluong,soluongdamua order by sl desc";
    	List<Sanpham> listdulich = new ArrayList<Sanpham>();
    	listdulich = spmodel.timsanpham(strSearch);
    	session.setAttribute("listdulich", listdulich);
    	
    	strSearch = "select * from quangcao where vitri = 'trai' or vitri = 'phai'";
    	QuangcaoModel qcmodel = new QuangcaoModel();
    	List <Quangcao> listqc = new ArrayList<Quangcao>();
    	listqc = qcmodel.getlistqc(strSearch);
    	session.setAttribute("listqc", listqc);
    	
    	RequestDispatcher view = request.getRequestDispatcher("index.jsp");
        view.forward(request, response);
        
    }
    
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request,response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
